package com.zzedu.integration.fileread.impl;

import com.zzedu.integration.fileread.FileReadFactoryService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * DOCX试卷处理器
 */
@Service
public class ReadDocxFileReadFactoryServiceImpl implements FileReadFactoryService {
    Logger log = LoggerFactory.getLogger(ReadDocxFileReadFactoryServiceImpl.class);
    static List<String> exInclude = new ArrayList<>();

    static {
        exInclude = new ArrayList<>();
        for (char letter = 'A'; letter <= 'Z'; letter++) {
            exInclude.add(String.valueOf(letter));
        }
        exInclude.add("'");
        exInclude.add("’");
    }

    @Override
    public List<String> readFile(InputStream inputStream) {
        List<String> wordAllList = new ArrayList<>();
        try (
                InputStream fis = inputStream;
                XWPFDocument document = new XWPFDocument(fis)) {
            List<String> questions = new ArrayList<>();
            for (IBodyElement element : document.getBodyElements()) {
                if (element instanceof XWPFParagraph) {
                    // 处理段落内容
                    XWPFParagraph paragraph = (XWPFParagraph) element;
                    String text = paragraph.getText().trim();
                    if (!text.isEmpty()) {
                        questions.add(text);
                    }
                } else if (element instanceof XWPFTable) {
                    // 处理表格内容
                    XWPFTable table = (XWPFTable) element;
                    for (XWPFTableRow row : table.getRows()) {
                        for (XWPFTableCell cell : row.getTableCells()) {
                            String text = cell.getText();
                            if (!text.isEmpty()) {
                                questions.add(text);
                            }
                        }
                    }
                }
            }
            // 提取题目中的单词
            for (String question : questions) {
                log.info("内容: " + question);
                List<String> wordList = extractWords(question);
                log.info("提取单词:" + wordList);
                if (CollectionUtils.isNotEmpty(wordList)) {
                    wordAllList.addAll(wordList);
                }
            }
        } catch (Exception e) {
            log.error("错误信息:" + e.getMessage());
        }
        return wordAllList;
    }



    private List<String> extractWords(String sentence) {
        // 使用正则表达式匹配单词
        Pattern wordPattern = Pattern.compile("\\b[a-zA-Z]+(('|’)[a-z]+)?\\b");
        Matcher matcher = wordPattern.matcher(sentence);
        List<String> wordList = new ArrayList<>();
        while (matcher.find()) {
            String w = matcher.group();
            if (StringUtils.isNotBlank(w) && !exInclude.contains(w.toUpperCase()) && !w.contains("’") && !w.contains("'")) {
                wordList.add(matcher.group());
            }
        }
        return wordList;
    }
    @Override
    public boolean isRead(String fileName) {
        return fileName.toLowerCase().endsWith(".docx");
    }
}
